/*
 * Copyright 2010 EES GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.eesgmbh.gimv.samples.googlechartapi.client.calendar;

import java.util.Date;

import org.eesgmbh.gimv.client.presenter.CalendarPresenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ShowRangeHandler;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.datepicker.client.DatePicker;

/**
 *
 * View which displays the currently selected date and a button to toggle a
 * calendar.
 *
 * @author Sascha Hagedorn - EES GmbH - s.hagedorn@ees-gmbh.de
 *
 */
public class CustomCalendarViewImpl implements CalendarPresenter.View {

	private final DatePicker datePicker;
	private Label dateLabel;
	private PopupPanel popup;
	private Image calendarImage;
	private DateTimeFormat format;

	public CustomCalendarViewImpl(DatePicker datePicker, Label dateLabel, Image calendarImage) {
		this.datePicker = datePicker;

		this.dateLabel = dateLabel;
		this.calendarImage = calendarImage;

		this.popup = new PopupPanel();
		this.popup.setWidget(datePicker);
		this.popup.hide();

		calendarImage.addClickHandler(new CustomCalendarViewImplEventHandler());

		format = DateTimeFormat.getFormat("dd.MM.yyyy");
	}

	public void setDate(Date date) {
		this.datePicker.setValue(date, false);
		this.datePicker.setCurrentMonth(date);

		this.dateLabel.setText(format.format(date));
		this.popup.hide();
	}

	private void onClick(ClickEvent event) {
		if (this.popup.isShowing()) {
			this.popup.hide();
		} else {
			this.popup.setPopupPosition(calendarImage.getAbsoluteLeft() + calendarImage.getOffsetWidth() + 10, calendarImage.getAbsoluteTop());
			this.popup.show();
		}
	}

	private class CustomCalendarViewImplEventHandler implements ClickHandler {

		public void onClick(ClickEvent event) {
			CustomCalendarViewImpl.this.onClick(event);
		}

	}

	public void disableDate(Date date) {
		this.datePicker.setTransientEnabledOnDates(false, date);
	}

	public void addValueChangeHandler(ValueChangeHandler<Date> valueChangeHandler) {
		this.datePicker.addValueChangeHandler(valueChangeHandler);
	}

	public void addShowRangeHandler(ShowRangeHandler<Date> showRangeHandler) {
		this.datePicker.addShowRangeHandler(showRangeHandler);
	}
}
